package com.hw.one.collect.util;

import org.apache.commons.lang3.StringUtils;

import java.net.URLEncoder;

/**
 * Created by Administrator on 2016/7/11.
 */
public class ParamsUtil {
    public synchronized static String encode(String params) throws Exception{
        return encode(params,"UTF-8");
    }
    public synchronized static String encode(String params,String charSet) throws Exception{
        if(StringUtils.isBlank(charSet)){
            charSet = "UTF-8";
        }
        if(StringUtils.isNotBlank(params)){
            String[] paramsArr =  StringUtils.split(params,"&");
            for (int i = 0 ;i < paramsArr.length ;i++){
                String param = paramsArr[i];
                if(StringUtils.isBlank(param)){
                    continue;
                }
                String name = param.split("=")[0];
                String value = param.split("=")[1];
                String paramEncoded = name + "=" + URLEncoder.encode(value,"UTF-8");
                paramsArr[i] = paramEncoded;
            }
            params = StringUtils.join(paramsArr,"&");
        }
        return params;
    }
}
